using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BHXH.Core.Domain;
using BHXH.Core.IService;
using FX.Data;
using NHibernate;

namespace BHXH.Core.ServiceImp
{
    public class thongtinhoadonService: BaseService<thongtinhoadon ,int>,IthongtinhoadonService
    {
        ISession mySession;
        public thongtinhoadonService(string sessionFactoryConfigPath)
            : base(sessionFactoryConfigPath)
        {}


        public thongtinhoadon GetHoaDonByID(string MaSoThue)
        {
            using (mySession = this.NHibernateSession.SessionFactory.OpenSession())
            {
                IList<thongtinhoadon> query = mySession.CreateQuery("FROM thongtinhoadon as t where t.MaSoThue like :m")
                    .SetParameter("m", "%" + MaSoThue + "%")
                    .List<thongtinhoadon>();
                thongtinhoadon tthd = new thongtinhoadon();
                foreach (thongtinhoadon item in query)
                {
                    tthd = item;
                }
                return tthd;
            }
        }
        public bool UpdateHoaDon(thongtinhoadon tthd)
        {
            using (mySession = this.NHibernateSession.SessionFactory.OpenSession())
            {
                mySession.Update(tthd);
                CommitChanges();
                mySession.Flush(); 
                //var a = mySession.CreateQuery("Update thongtinhoadon set TenDonVi = :tdv, MaSoThue = :mst, SoTaiKhoan = :stk, HoTen = :ht, DiaChi = :dc, SoDienThoai = :sdt, Email = :em where ID = :id")
                //        .SetParameter("tdv", tthd.TenDonVi)
                //        .SetParameter("mst", tthd.MaSoThue)
                //        .SetParameter("stk", tthd.SoTaiKhoan)
                //        .SetParameter("ht", tthd.HoTen)
                //        .SetParameter("dc", tthd.DiaChi)
                //        .SetParameter("sdt", tthd.SoDienThoai)
                //        .SetParameter("em", tthd.Email)
                //        .SetParameter("id", tthd.ID)
                //        .ExecuteUpdate();

                return true;
            }

        }
        public bool AddHoaDon(thongtinhoadon tthd)
        {
            using(mySession = this.NHibernateSession.SessionFactory.OpenSession())
            {
                var a = CreateNew(tthd);
                CommitChanges();
                return true;
            }
        }
    }

}